package java101;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class NearestFib {

	static Scanner sc = new Scanner(System.in);	
	
	public static void main(String[] args) {
		
		int[] fib = new int[25];
		
		for (int i = 0; i < 25; i++) {
			fib[i] = i < 2? i : fib[i-2] + fib[i-1];
			//Is i < 2? If so, print i. Otherwise, print fib[i-2] + fib[i-1].
		}
		
		String input = JOptionPane.showInputDialog
		("Enter a number between 0 and 10,000: ");
		int n = Integer.parseInt(input);
		
		if (n < 0 || n > 10000) {
			System.out.println("You didn't enter a number between 0 and 10,000!");
			System.exit(0);
		}
		
		for (int i = 1; i < 25; i++) {
			
			if (n == fib[i] || n == fib[i-1]) {
				System.out.println("Congrats! " + n + " is a Fib number!");
				break;
			}
			
			else if (fib[i-1] < n && n < fib[i]) {
				
				int diffa = n - fib[i-1];
				
				int diffb = fib[i] - n;
				
				if (diffa < diffb) {
					System.out.println("The nearest Fib is " + fib[i-1] + ".");
				}
				
				else if (diffa == diffb) {
					System.out.println(n + " is exactly between " + fib[i-1] + " and " + fib[i] + ".");
				}
				
				else {
					System.out.println("The nearest Fib is " + fib[i] + ".");
				}
				break;
			}
		}

	}

}
